﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using NLog;

using Sonic.Framework;
using System.IO;

namespace SonicGUI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private Thread t;

        private FileInfo config = null;

        private void button1_Click(object sender, EventArgs e)
        {
            if (config == null)
            {
                if (SelectConfigFile())
                {
                    Start();
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if(t != null)
                t.Abort();

            base.OnClosing(e);
        }

        private void Start()
        {
            SonicProcessor processor = SonicProcessorFactory.GetSonicProcessor(config.FullName);

            processor.LoggerName = LogManager.GetCurrentClassLogger().Name;

            t = new Thread(new ThreadStart(processor.Process));

            t.Start();
        }

        private bool SelectConfigFile()
        {
            openFileDialog1.ShowDialog();

            FileInfo selectedConfig = new FileInfo(openFileDialog1.FileName);

            if (selectedConfig.Exists)
            {
                config = selectedConfig;
                lblConfigPath.Text = config.FullName;
            }

            return selectedConfig.Exists;
        }
    }
}
